import axios from "axios";
import { useUsersStore } from "../store/user.js"

// const store = useUsersStore()
// console.log(store)
const instance = axios.create({
  baseURL: "http://43.139.124.13:8888",
  timeout: 5000,
});
// const store = useUsersStore()
// Add a request interceptor
instance.interceptors.request.use(
  function (config) {
    // Do something before request is sent
    // config.headers.token = getToken();
      const login:string = localStorage.getItem("login")
      config.headers['token'] = JSON.parse(login)?.user?.token
    return config;
  },
  function (error) {
    // Do something with request error
    // @ts-ignore
      return Promise.reject(error);
  }
);

// Add a response interceptor
instance.interceptors.response.use(
  function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
      if(response.config.headers?.token){
          sessionStorage.setItem("login","true")
      }else {
          localStorage.clear()
      }
    return response;
  },
  function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    // @ts-ignore
      return Promise.reject(error);
  }
);
// get请求
export const get = (url: string, params: any) =>
  instance.get<any>(url, { params });
// post请求
export const post = (url: string, data: any) => instance.post<any>(url, data);
// put请求
export const put = (url: string, data: any) => instance.put<any>(url, data);
// del请求
export const del = (url: string, data: any) => instance.delete<any>(url, data);
